<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Routing\Router;
use Nwidart\Modules\Facades\Module;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Modules\Admin\Services\PermissionService;

class RefreshPermissionCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'refreshPermission';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '刷新权限【新增开启权限的路由后需要刷新权限】php artisan refreshPermission';

    /**
     * The router instance.
     *
     * @var \Illuminate\Routing\Router
     */
    protected $router;

    /**
     * Create a new route command instance.
     *
     * @param \Illuminate\Routing\Router $router
     *
     * @return void
     */
    public function __construct(Router $router)
    {
        parent::__construct();

        $this->router = $router;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $search_alias = $this->ask("搜索权限关键词：（如：控制台，默认：全部）?");

        $reset = $this->ask("是否重置权限（yes/no）?", 'no');
        if ($reset == 'yes') {
            $this->info("正在删除权限...");
            $del_count = PermissionService::getInstance()->delPermission('all', $search_alias);
            $this->info("删除权限成功，共 {$del_count} 个权限");
        }

        $this->info("【正在刷新权限...");
        $permissions = PermissionService::getInstance()->refreshPermissionByRoute($this->router, 'admin', $search_alias);
        $this->info("刷新权限成功，共 " . count($permissions) . " 个权限");
        $this->info("操作完成！");
    }
}
